@extends('layout.member')

@section('body')
    <div class="panel active">
        <div class="panel-head">
            <h4>编辑物料</h4>
        </div>
        <div class="panel-body">
            <form class="layui-form layui-form-pane">
                @include('member.materiel.body')
            </form>
        </div>
    </div>
@endsection
@section('js')
    <script type="text/javascript" src="{{asset('js/member/distpicker.data.js')}}"></script>
    <script type="text/javascript">
        layui.use(['form', 'upload'], function () {
            var form = layui.form
                , layer = layui.layer
                , upload = layui.upload;
            //区域切换
            form.on('select(province)', function (data) {
                $("#province").val(data.value).change();
                form.render();
            });
            form.on('select(city)', function (data) {
                $("#city").val(data.value).change();
                form.render();
            });
            //营业执照上传
            var licenseUpload = upload.render({
                elem: '#licenseUpload'
                , url: '{{route('upload','license')}}'
                , size: 1024
                , accept: 'images'
                , headers: {'X-CSRF-TOKEN': '{{csrf_token()}}'}
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#license').attr('src', result); //图片链接（base64）
                    });
                }, done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg(res.message);
                    }
                    $('#license').attr('data-img', res.data);
                    return layer.msg(res.message);
                }, error: function () {
                    //演示失败状态，并实现重传
                    var licenseText = $('#licenseText');
                    licenseText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    licenseText.find('.demo-reload').on('click', function () {
                        licenseUpload.upload();
                    });
                }
            });
            //报备单上传
            var reportUpload = upload.render({
                elem: '#reportUpload'
                , url: '{{route('upload','report')}}'
                , size: 1024
                , accept: 'images'
                , headers: {'X-CSRF-TOKEN': '{{csrf_token()}}'}
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#report').attr('src', result); //图片链接（base64）
                    });
                }, done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg(res.message);
                    }
                    $('#report').attr('data-img', res.data);
                    return layer.msg(res.message);
                }, error: function () {
                    //演示失败状态，并实现重传
                    var reportText = $('#reportText');
                    reportText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    reportText.find('.demo-reload').on('click', function () {
                        reportUpload.upload();
                    });
                }
            });
            form.on('radio(type)', function (data) {
                if (data.value == 1) {
                    $('.materiel_upload').show();
                } else {
                    $('.materiel_upload').hide();
                }
            });
            //自定义验证规则
            form.verify({
                OPhone: [
                    /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/
                    , '座机号格式错误'
                ]
                , SPhone: function (value) {
                    if (checkTel(value) === false) {
                        layer.msg('坐席号码只能为手机号或者座机号');
                    }
                }
            });
            //监听提交
            form.on('submit(submitMateriel)', function (data) {
                var param = data.field;
                param['license'] = $('#license').attr('data-img');
                param['report'] = $('#report').attr('data-img');
                $.post("{{route('materiel.update',$materiels->id)}}",
                    {
                        _token: '{{csrf_token()}}',
                        param: param,
                        _method: 'PUT'
                    },
                    function (data) {
                        if (data.status == 200) {
                            layer.msg(data.msg);
                            setTimeout(function () {
                                window.location.href = data.data;
                            }, 1500);
                        } else {
                            layer.msg(data.msg);
                        }
                    });
                return false;
            });
        });

        function checkTel(tel) {
            var mobile = /^(13[0-9]|14[5|7|9]|15[0|1|2|3|5|6|7|8|9]|16[6]|17[0|1|2|3|5|6|7|8]|18[0-9]|19[8|9])\d{8}$/,
                phone = /^0\d{2,3}-?\d{7,8}$/;
            return mobile.test(tel) || phone.test(tel);
        }

        $(function () {
            var $distpicker = $('#region');
            $distpicker.distpicker({
                province: '{{empty($materiels->province) ? "山西省" : $materiels->province}}',
                city: '{{empty($materiels->city) ? "太原市" : $materiels->city}}'
            });
        });
    </script>
@append